package thread;

public class SyncDemo4 {
    public static void main(String[] args) {
        Boo b = new Boo();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                b.methodA();
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                b.methodB();
            }
        };

        t1.start();
        t2.start();
    }
}

class Boo {
    public synchronized void methodA() {
        Thread t = Thread.currentThread();
        System.out.println(t.getName() + "methodA正在执行");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName() + "methodA方法执行完毕");
    }

    public void methodB() {
        Thread t = Thread.currentThread();
        // synchronized ("this") {   对象是字符串
        synchronized (this) {
            System.out.println(t.getName() + "methodB正在执行");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t.getName() + "methodB方法执行完毕");
        }
    }
}
